﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObjectGraph.Core
{
    public delegate TFinalType Translator<TInitialType, TFinalType>( TInitialType arg );

    public class TranslatingEnumerator<TInitialType, TFinalType> : IEnumerator<TFinalType>
    {
        Translator<TInitialType,TFinalType> m_translator;
        IEnumerator<TInitialType> m_enumerator;

        public TranslatingEnumerator(Translator<TInitialType, TFinalType> translator
            , IEnumerator<TInitialType> enumerator)
        {
            m_translator = translator;
            m_enumerator = enumerator;
        }


        #region IEnumerator<TFinalType> Members

        public TFinalType Current
        {
            get { return m_translator(m_enumerator.Current); }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            m_enumerator.Dispose();
        }

        #endregion

        #region IEnumerator Members

        object System.Collections.IEnumerator.Current
        {
            get { return Current; }
        }

        public bool MoveNext()
        {
            return m_enumerator.MoveNext();
        }

        public void Reset()
        {
            m_enumerator.Reset();
        }

        #endregion
    }

    public class TranslatingEnumerable<TInitialType, TFinalType> : IEnumerable<TFinalType>
    {
        Translator<TInitialType, TFinalType> m_translator;
        IEnumerable<TInitialType> m_enumerable;

        public TranslatingEnumerable(Translator<TInitialType, TFinalType> translator
            , IEnumerable<TInitialType> enumerable)
        {
            m_translator = translator;
            m_enumerable = enumerable;
        }

        #region IEnumerable<TFinalType> Members

        public IEnumerator<TFinalType> GetEnumerator()
        {
            return new TranslatingEnumerator<TInitialType, TFinalType>(m_translator, m_enumerable.GetEnumerator());
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
